<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input>
<button>按钮</button>
<ul></ul>
<script>
    connect();
    function connect() {
        // 创建websocket
        ws = new WebSocket("ws://" + document.domain + ":7272");//这个WebSocket类是自定义的
        // 当socket连接打开时，输入用户名
        ws.onopen = onopen;
        // 当有消息时根据消息类型显示不同信息
        ws.onmessage = onmessage;
        ws.onclose = function () {
            console.log("连接关闭，定时重连");
            connect();
        };
        ws.onerror = function () {
            console.log("出现错误");
        };
    }

    function onopen() {
        console.log('链接上了');
        ws.send('{msg:链接上了}');
    };


    function onmessage(e) {
        console.log(e.data);
    };
</script>
</body>
</html>